<!-- start: Wrapper -->
<?php
$auth = $this->auth;
$translator = \Velacolib\Utility\Utility::translate();

?>
<div class="page-header vela-col" id="banner" style="text-align: center">
    <h4>Quản lý tin đăng</h4>

    <div class="pull-right">


        <form action="/user/topic" method="get" style="width: 200px;">
            <select class="form-control" onchange="this.form.submit()" name="is_p">
                <option selected>Lọc nội dung</option>
                <option value="yes">Công khai</option>
                <option value="no">Ẩn</option>
            </select>
        </form>
    </div>
    <div style="clear: both"></div>

</div>

<!-- ================================== content body ================================== -->
<div class="content-body">
    <div class="row" style="margin: 10px 0px 10px 0px">
        <div class="col-md-12">
            <form action="/user/topic" method="get">
                <div class="input-group">
                    <!-- USE TWITTER TYPEAHEAD JSON WITH API TO SEARCH -->
                    <input class="form-control" id="manage_topic_search" name="q" placeholder="Tìm kiếm..." required>
                    <span class="input-group-btn">
                        <button type="submit" class="btn btn-default" style="padding: 9px 12px"><i
                                class="glyphicon glyphicon-search"></i></button>
                    </span>
                </div>
            </form>
        </div>

        <?php
        if (count($topics) < 1) {
            ?>
            <div class="col-sm-12 col-md-12 col-lg-12 vela-topic-item" style="min-height: 500px">
                <div class="well well-lg">
                    <h3>Chào ,<?php echo $auth->getIdentity()->name ?> !</h3>

                    <p>Bạn có sản phẩm cần bán? Hoặc cần mua? Minimall sẽ giúp bạn tiết kiệm thời gian mua bán</p>

                    <a href="/raovat/dangtin" class="btn btn-danger">Đăng tin ngay</a>

                </div>
            </div>

        <?php } ?>
        <?php foreach ($topics as $itemtopics) {

            ($itemtopics->is_public == 'yes') ? $is_public = $translator->translate('Public') : $is_public = $translator->translate('Hidden');
            if($itemtopics->is_public == 'yes'){
                $onclick = "antin(".$itemtopics->id.",'no')";
                $text = $translator->translate('Hidden');
            } else{
                $onclick = "antin(".$itemtopics->id.",'yes')";

                $text = $translator->translate('Public');
            }


            ?>
            <div class="col-sm-12 col-md-12 col-lg-12 vela-topic-item">
                <div class="col-lg-2 col-sm-2 col-md-2  topic-image hidden-xs">
                    <a href="/raovat/chitiet/<?php echo $itemtopics->slug ?>/<?= $itemtopics->id ?>">
                        <img style="width: 98px;height: 80px" src="<?php echo $itemtopics->images ?>">
                    </a>
                </div>
                <div class="col-lg-10 col-sm-10 col-md-10">
                    <div class="topic-title"><a
                            href="/raovat/chitiet/<?php echo $itemtopics->slug ?>/<?= $itemtopics->id ?>">
                            [ <?php echo Velacolib\Utility\Utility::getProductType($itemtopics->topic_type); ?></php> ]
                            <?php echo $itemtopics->title; ?></a></div>
                    <div class="topic-info">
                        <span>Tình trạng: <strong
                                class="is_public_<?= $itemtopics->id ?>"><?= $is_public ?></strong> </span>

                            <span class="time pull-right">

<a href="/raovat/edit/<?= $itemtopics->slug ?>/<?= $itemtopics->id ?>" class="btn-sm btn-mod-black">
    <?= $translator->translate('Edit'); ?>
</a>
<a class="btn-sm btn-mod-black" href="javascript: void(0)" onclick="<?=$onclick?>">
    <?=$text?>
</a>
<a class="btn-sm btn-mod-black" href="javascript: void(0)" onclick="uptin(<?= $itemtopics->id ?>)">
    <?= $translator->translate('Refresh post'); ?>
</a>
                            </span>
                    </div>
                    <div class="category">
                        <?php echo Velacolib\Utility\Utility::time_stamp($itemtopics->create_date); ?>
                    </div>
                </div>
            </div>
        <?php } ?>

    </div>

    <!--paginator -->
    <div class="row-fluid">
        <?php
        // add at the end of the file after the table
        echo $this->paginationControl(
        // the paginator object
            $topics,
            // the scrolling style; see http://zf2.readthedocs.org/en/release-2.1.4/modules/zend.paginator.usage.html#rendering-pages-with-view-scripts
            'sliding',
            // the partial to use to render the control
            array('partial/paginator.phtml', 'Topic'),
            // the route to link to when a user clicks a control link
            array(
                'route' => 'frontenduser',
                'child_route' => array(
                    'controller' => 'user',
                    'action' => 'topic',
                    'param' => $param,
                ),

                'search_val' => $search_val


            )
        );
        ?>
    </div>
    <!--end paginator -->
</div>
<script src="<?php echo $this->basePath(); ?>/templates/front/js/js_frontend/manager_topic.js"></script>
<script>
    function uptin(id_tin) {
        $.ajax({
            type: 'post',
            url: '/raovat/uptopic',
            dataType: 'html',
            data: {id: id_tin},
            beforeSend: function () {
                jnotify('<?=$translator->translate('loading')?>');
            }, success: function (response) {
                response = JSON.parse(response);
                if (response.result) {
                    jsuccess(response.message);
                } else {
                    jerror(response.message);
                }
            }
        })

    }
    ;

    function antin(id_tin,is_public) {
        $.ajax({
            type: 'post',
            url: '/raovat/hidden-topic',
            dataType: 'html',
            data: {id: id_tin,is_public:is_public},
            beforeSend: function () {
                jnotify('<?=$translator->translate('loading')?>');
            }, success: function (response) {
                response = JSON.parse(response);
                if (response.result) {
                    if(is_public == 'no'){
                        $(".is_public_" + id_tin).text("Ẩn");
                    } else{
                        $(".is_public_" + id_tin).text("Công khai");
                    }

                    jsuccess(response.message);
                } else {
                    jerror(response.message);
                }
            }
        })

    }
    ;

</script>